<template>
    <!--弹窗组件封装-->
    <div class="message" v-show="isShow">
        <div class="title" @click="confirm">{{title}}</div>
        <div class="content" @click="confirm">
            {{message}}
        </div>
        <div class="closeButton" @click="close">
            <i class="el-icon-close"></i>
        </div>
    </div>
</template>

<script>
export default {
    name: "Message",
    props: {
        title: {
            type: String,
            default: '标题'
        },
        message: {
            type: String,
            default: '这是弹框内容'
        },
        time: {
            type: Number,
            default: 0
        },
    },
    data(){
        return{
            isShow:false,
            resolve:'',
            reject:''
        }
    },
    mounted(){
        if (this.time>0){
            setTimeout(() => {
                this.remove()
            },this.time);
        }
    },
    methods:{
        confirm: function () {
            this.isShow = false;
            this.resolve('confirm');
            this.remove();
        },
        show(){
            this.isShow = true;
            this.promise = new Promise((resolve, reject) => {
                this.resolve = resolve;
                this.reject = reject;
            });
            // 返回promise对象
            return this.promise;
        },
        close(){
            this.isShow = false;
            this.reject('cancel');
            this.remove();
        },
        remove: function () {
            setTimeout(() => {
                this.destroy();
            }, 300);
        },
        destroy: function () {
            this.$destroy();
            document.body.removeChild(this.$el);
        }
    }
}
</script>

<style scoped lang="less">
    .message{
        position: fixed;
        bottom: 30px;
        right: 50px;
        color: #333;
        width: 24%;
        box-sizing: border-box;
        border-radius:5px;
        padding: 10px;
        background-color: #fff;
        z-index: 9999;
        cursor: pointer;
        animation: shake 1s infinite;
        .title{
            font-weight: 700;
            font-size: 16px;
            color: #303133;
            margin: 0;
            width: calc( 100% - 36px );
        }
        .content{
            font-size: 14px;
            line-height: 21px;
            margin: 6px 0 0;
            color: #606266;
            width: calc( 100% - 36px );
        }
        .closeButton{
            position: absolute;
            top: 18px;
            right: 15px;
            cursor: pointer;
            color: #909399;
            font-size: 16px;
        }
    }

    @keyframes shake { /* 水平抖动，核心代码 */
        10%, 90% { transform: translate3d(-1px, 0, 0); }
        20%, 80% { transform: translate3d(+1px, 0, 0); }
        30%, 70% { transform: translate3d(-1px, 0, 0); }
        40%, 60% { transform: translate3d(+1px, 0, 0); }
        50% { transform: translate3d(-1px, 0, 0); }
    }
    @keyframes mymove {
        50% {top: 300px;}
    }
</style>